package com.vegas.netspider.memdb.dao;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.JdbcTemplate;

import com.vegas.netspider.db.SportMatchDbHandler;
import com.vegas.netspider.dto.SportMatch;
import com.vegas.netspider.memdb.DbOperator;
import com.vegas.netspider.memdb.MemDBCloseListener;

public abstract class MemdbDao implements MemDBCloseListener{
	
	protected JdbcTemplate dao;
	private SportMatchDbHandler dbHandler;
	
	protected Logger logger = Logger.getLogger(MemdbDao.class);
	
	public void setDbPool(DbOperator db){
		dao = new JdbcTemplate(db.getDataSource());
		db.registListener(this);
		
		
	}
	
	protected void onInsert(String tableName,SportMatch m){
		try{
			if(tableName.indexOf("ft")>-1){
				dbHandler.saveFt(m);
			}else if(tableName.indexOf("bk")>-1){
				dbHandler.saveBk(m);
			}
		}catch (Exception e) {
			e.printStackTrace();
			logger.error(e);
		}
	}
	
	public void setDbHandler(SportMatchDbHandler dbHandler) {
		this.dbHandler = dbHandler;
	}
}
